import api from "@/api/api";

const MiniApp = window.MiniApp

function init() {
  let mp = MiniApp.createMusicPlayer({isInner: true})
  return mp
}

const musicPlayer = init()

const getMusicPlayer = () => musicPlayer

const getPlayerInfo=()=>musicPlayer.info

const playMusic = (music_id, music_name) => {
  return new Promise((resolve) => {
    musicPlayer.setData({
      album_audio_ids: [music_id],
      success() {
        MiniApp.showToast({title:"播放成功"})
        api.playMusic(music_id, music_name)
        resolve()
      }
    })
  })

}

const stopMusic=()=>{
  return new Promise(resolve => {
    musicPlayer.stop({
      success(){
        MiniApp.showToast({title:"停止播放"})
        resolve()
      }
    })
  })

}

const music={
  getMusicPlayer,
  playMusic,
  stopMusic,
  getPlayerInfo
}

export default music